package com.chaomian.plus.starter.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 基础实体类--默认数据库自增长
 */
@Data
public abstract class BaseEntity<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(required = false, value = "主键")
    @TableId(value = "id", type = IdType.AUTO)
    @JsonSerialize(using = ToStringSerializer.class)
    protected T id;

    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    @ApiModelProperty(required = false, value = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")
    private LocalDateTime createdTime;

    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    @ApiModelProperty(required = false, value = "修改时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")
    private LocalDateTime updatedTime;


    @Override
    public boolean equals(Object obj) {
        if (null == obj) {
            return false;
        }
        if (this == obj) {
            return true;
        }
        if (!getClass().equals(obj.getClass())) {
            return false;
        }
        @SuppressWarnings("unchecked") BaseEntity<T> that = (BaseEntity<T>) obj;
        return null != this.getId() && this.getId().equals(that.getId());
    }


    @Override
    public int hashCode() {
        return getId() == null ? super.hashCode() : getId().hashCode();
    }

    @Override
    public String toString() {
        return "{" + "id=" + id + ", createdTime=" + createdTime + ", updatedTime=" + updatedTime + '}';
    }
}
